package com.example.fish.controller.admin;

import com.example.fish.controller.BaseController;
import com.example.fish.pojo.Promotion;
import com.example.fish.pojo.User;
import com.example.fish.service.UserService;
import com.example.fish.service.admin.AdminService;
import com.example.fish.service.admin.dto.BlogDTO;
import com.example.fish.service.admin.dto.SecretDTO;
import com.example.fish.service.admin.dto.UserInfoDTO;
import com.example.fish.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {
    /**
     * 管理员
     */
    @Autowired
    private UserService userService;
    @Resource
    private AdminService adminService;
    @PostMapping("/login")
    private JsonResult<User> login(String username, String password, HttpSession session){
        User user = (User) userService.adminLogin(username, password);
        session.setAttribute("user", user);
        return new JsonResult<>(200,"success",user);
    }

    @GetMapping("/total")
    private JsonResult<List<Integer>> getTotal(){
        List<Integer> total = adminService.getTotal();
        return new JsonResult<>(200,"success",total);
    }

    @GetMapping("/provence")
    private JsonResult<List<Map<String,String>>> provence(){
        List<Map<String, String>> provence = adminService.getProvence();
        return new JsonResult<>(200,"success",provence);
    }

    @PostMapping("/info")
    private JsonResult<PageInfo<UserInfoDTO>> info(String username,Integer sex,String redDate,Integer pageNum){
        PageInfo<UserInfoDTO> users = adminService.getUsers(username, sex, redDate, pageNum);
        return new JsonResult<>(200,"success",users);
    }

    @PostMapping("/del")
    private JsonResult<Void> del(Integer id){
        adminService.deleteUser(id);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/blog")
    private JsonResult<PageInfo<BlogDTO>> blog(Integer pageNum){
        PageInfo<BlogDTO> blogDTOPageInfo = adminService.agreeRank(pageNum);
        return new JsonResult<>(200,"success",blogDTOPageInfo);
    }
    @PostMapping("/blogtime")
    private JsonResult<PageInfo<BlogDTO>> blogtime(Integer pageNum){
        PageInfo<BlogDTO> blogDTOPageInfo = adminService.timeRank(pageNum);
        return new JsonResult<>(200,"success",blogDTOPageInfo);
    }

    @PostMapping("/bloglike")
    private JsonResult<PageInfo<BlogDTO>> like(Integer pageNum,String title){
        PageInfo<BlogDTO> blogByTitle = adminService.getBlogByTitle(title, pageNum);
        return new JsonResult<>(200,"success",blogByTitle);
    }
    @PostMapping("/blog/delete")
    private JsonResult<Void> delete(Integer id){
        adminService.deleteBlog(id);
        return new JsonResult<>(200,"success",null);
    }

    @PostMapping("/secret")
    private JsonResult<PageInfo<SecretDTO>> list(Integer pageNum){
        PageInfo<SecretDTO> secretDTOPageInfo = adminService.secretList(pageNum);
        return new JsonResult<>(200,"success",secretDTOPageInfo);
    }
    @PostMapping("/secret/type")
    private JsonResult<PageInfo<SecretDTO>> type(String type,Integer pageNum){
        PageInfo<SecretDTO> secretDTOPageInfo = adminService.secretListByType(type, pageNum);
        return new JsonResult<>(200,"success",secretDTOPageInfo);
    }
    @PostMapping("/secret/keyword")
    private JsonResult<PageInfo<SecretDTO>> keyword(String keyword,Integer pageNum){
        PageInfo<SecretDTO> secretDTOPageInfo = adminService.secretListByKey(keyword, pageNum);
        return new JsonResult<>(200,"success",secretDTOPageInfo);
    }

    @PostMapping("/secret/delete")
    private JsonResult<Void> deleteSecret(Integer id){
        adminService.deleteSecret(id);
        return new JsonResult<>(200,"success",null);
    }

    @PostMapping("/promotion/save")
    private JsonResult<Void> save(String content,String title,String type){
        adminService.savePromotion(title,content,type);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/promotion/get")
    private JsonResult<PageInfo<Promotion>> get(Integer pageNum){
        PageInfo<Promotion> promotion = adminService.getPromotion(pageNum);
        return new JsonResult<>(200,"success",promotion);
    }
    @PostMapping("/promotion/delete")
    private JsonResult<Void> deletePromotion(Integer id){
        adminService.deletePromotion(id);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/promotion/update")
    private JsonResult<Void> updateStatus(Integer id){
        adminService.updatePromotion(id);
        return new JsonResult<>(200,"success",null);
    }
    @GetMapping("/promotion/getShow")
    private JsonResult<List<Promotion>> getList(){
        return new JsonResult<>(200,"success",adminService.getAllPromotion());
    }
    @GetMapping("/promotion/{id}")
    private JsonResult<Promotion> getById(@PathVariable Integer id){
        return new JsonResult<>(200,"success",adminService.getById(id));
    }

}
